package com.mediacloud.app.newsmodule.fragment.baoliao.frag

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.mediacloud.app.model.fragment.BaseBottomSheetFragment
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.fragment.baoliao.model.add.Catalog
import kotlinx.android.synthetic.main.baoliao_lanmu_layout.*
import org.greenrobot.eventbus.EventBus

class BaoLiaoLanMuSheet() : BaseBottomSheetFragment(), OnLanMuSelected {

    var adapter: BaoLiaoLanMuAdapter? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.baoliao_lanmu_layout, container, false)

    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return BottomSheetDialog(requireContext(), R.style.transparentBottomSheetStyle);
    }

    override fun getHeight(): Int {
        return 1000
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        listContainer.layoutManager = LinearLayoutManager(requireContext())
        adapter = BaoLiaoLanMuAdapter(this)
        listContainer.adapter = adapter

        arguments?.getSerializable("CATALOG")?.let {
            adapter?.selected = it as Catalog
        }
        arguments?.getSerializable("CATALOG_LIST")?.let {
            adapter?.data?.clear()
            adapter?.data?.addAll(it as ArrayList<Catalog>)
            adapter?.notifyDataSetChanged()
        }

    }

    override fun onLanMuSelected(catalog: Catalog?) {
        dismiss()
        EventBus.getDefault().post(BLCatalogEvent(catalog))
    }
}


interface OnLanMuSelected {
    fun onLanMuSelected(catalog: Catalog?)
}


class BaoLiaoLanMuHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val title: TextView = itemView.findViewById(R.id.title)
    val checkBox: CheckBox = itemView.findViewById(R.id.checkBox)
}


class BaoLiaoLanMuAdapter(val listener: OnLanMuSelected) : RecyclerView.Adapter<BaoLiaoLanMuHolder>() {
    val data: MutableList<Catalog> = ArrayList()
    var selected: Catalog? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaoLiaoLanMuHolder {
        return BaoLiaoLanMuHolder(LayoutInflater.from(parent.context).inflate(R.layout.bl_lanmu_item, parent, false))
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: BaoLiaoLanMuHolder, position: Int) {
        val d = data[position]
        holder.title.text = d.name
        holder.checkBox.isChecked = d == selected
        holder.itemView.setOnClickListener {
            selected = d
            notifyDataSetChanged()
            listener.onLanMuSelected(selected)
        }
        holder.checkBox.setOnClickListener {
            selected = d
            notifyDataSetChanged()
            listener.onLanMuSelected(selected)
        }
    }
}